clear

/*

use "C:\Local\数据\ex2005 data.dta"

gen west = strmatch(location,"*重庆*")

replace west = 1 if strmatch(location,"*四川*")

replace west = 1 if strmatch(location,"*贵州*")

replace west = 1 if strmatch(location,"*云南*")

replace west = 1 if strmatch(location,"*西藏*")

replace west = 1 if strmatch(location,"*陕西*")

replace west = 1 if strmatch(location,"*甘肃*")

replace west = 1 if strmatch(location,"*青海*")

replace west = 1 if strmatch(location,"*宁夏*")

replace west = 1 if strmatch(location,"*新疆*")

replace west = 1 if strmatch(location,"*内蒙古*")

replace west = 1 if strmatch(location,"*广西*")

gen central = strmatch(location,"*山西*")

replace central = 1 if strmatch(location,"*吉林*")

replace central = 1 if strmatch(location,"*黑龙江*")

replace central = 1 if strmatch(location,"*安徽*")

replace central = 1 if strmatch(location,"*江西*")

replace central = 1 if strmatch(location,"*河南*")

replace central = 1 if strmatch(location,"*湖南*")

replace central = 1 if strmatch(location,"*湖北*")

gen east = strmatch(location,"*北京*")

replace east = 1 if strmatch(location,"*天津*")

replace east = 1 if strmatch(location,"*河北*")

replace east = 1 if strmatch(location,"*辽宁*")

replace east = 1 if strmatch(location,"*上海*")

replace east = 1 if strmatch(location,"*江苏*")

replace east = 1 if strmatch(location,"*浙江*")

replace east = 1 if strmatch(location,"*福建*")

replace east = 1 if strmatch(location,"*山东*")

replace east = 1 if strmatch(location,"*广东*")

replace east = 1 if strmatch(location,"*海南*")

replace east = 0 if west == 1 & east == 1

replace east = 1 if strmatch(location,"*温州*")

replace east = 1 if strmatch(location,"*青岛*")

replace east = 1 if strmatch(location,"*厦门*")

replace east = 1 if strmatch(location,"*珠海*")

replace east = 1 if strmatch(location,"*汕头*")

replace east = 1 if strmatch(location,"*深圳*")

replace east = 1 if strmatch(location,"*秦皇岛*")

replace east = 1 if strmatch(location,"*无锡*")

replace east = 1 if strmatch(location,"*淄博*")

replace east = 1 if strmatch(location,"*常州*")

replace east = 1 if strmatch(location,"*宁波*")

replace east = 1 if strmatch(location,"*南京*")

replace east = 1 if strmatch(location,"*苏州*")

replace east = 1 if strmatch(location,"*烟台*")

replace east = 1 if strmatch(location,"*广州*")

replace east = 1 if strmatch(location,"*石家庄*")

replace east = 1 if strmatch(location,"*沈阳*")

replace east = 1 if strmatch(location,"*威海*")

replace east = 1 if strmatch(location,"*鞍山*")

replace east = 1 if strmatch(location,"*佛山*")

replace east = 1 if strmatch(location,"*南通*")

replace east = 1 if strmatch(location,"*保定*")

replace east = 1 if strmatch(location,"*潍坊*")

replace east = 1 if strmatch(location,"*大连*")

replace east = 1 if strmatch(location,"*杭州*")

replace east = 1 if strmatch(location,"*连云港*")

replace east = 1 if strmatch(location,"*湛江*")

replace east = 1 if strmatch(location,"*镇江*")

replace east = 1 if strmatch(location,"*福州*")

replace east = 1 if strmatch(location,"*济南*")

replace east = 1 if strmatch(location,"*惠州*")

replace east = 1 if strmatch(location,"*中山*")

replace central = 1 if strmatch(location,"*芜湖*")

replace central = 1 if strmatch(location,"*合肥*")

replace central = 1 if strmatch(location,"*大庆*")

replace central = 1 if strmatch(location,"*长春*")

replace central = 1 if strmatch(location,"*郑州*")

replace central = 1 if strmatch(location,"*武汉*")

replace central = 1 if strmatch(location,"*洛阳*")

replace central = 1 if strmatch(location,"*哈尔滨*")

replace central = 1 if strmatch(location,"*太原*")

replace central = 1 if strmatch(location,"*长沙*")

replace central = 1 if strmatch(location,"*南昌*")

replace central = 1 if strmatch(location,"*株州*")

replace central = 1 if strmatch(location,"*襄樊*")

replace west = 1 if strmatch(location,"*西安*")

replace west = 1 if strmatch(location,"*成都*")

replace west = 1 if strmatch(location,"*昆明*")

replace west = 1 if strmatch(location,"*乌鲁木齐*")

replace west = 1 if strmatch(location,"*贵阳*")

replace west = 1 if strmatch(location,"*绵阳*")

replace west = 1 if strmatch(location,"*宝鸡*")

replace west = 1 if strmatch(location,"*兰州*")

replace west = 1 if strmatch(location,"*杨陵*")

replace west = 1 if strmatch(location,"*石河子*")

replace west = 1 if strmatch(location,"*桂林*")

replace west = 1 if strmatch(location,"*北海*")

replace west = 1 if strmatch(location,"*包头*")

replace west = 1 if strmatch(location,"*南宁*")

replace east = 0 if east == 1 & central == 1

gen east1 = strmatch(location,"*北京*")

replace east1 = 1 if strmatch(location,"*天津*")

replace east1 = 1 if strmatch(location,"*河北*")

replace east1 = 1 if strmatch(location,"*辽宁*")

replace east1 = 1 if strmatch(location,"*山东*")

gen east2 = strmatch(location,"*上海*")

replace east2 = 1 if strmatch(location,"*浙江*")

replace east2 = 1 if strmatch(location,"*江苏*")

gen east3 = strmatch(location,"*广东*")

replace east1 = 1 if strmatch(location,"*青岛*")

replace east1 = 1 if strmatch(location,"*秦皇岛*")

replace east1 = 1 if strmatch(location,"*淄博*")

replace east1 = 1 if strmatch(location,"*烟台*")

replace east1 = 1 if strmatch(location,"*石家庄*")

replace east1 = 1 if strmatch(location,"*沈阳*")

replace east1 = 1 if strmatch(location,"*威海*")

replace east1 = 1 if strmatch(location,"*鞍山*")

replace east1 = 1 if strmatch(location,"*保定*")

replace east1 = 1 if strmatch(location,"*潍坊*")

replace east1 = 1 if strmatch(location,"*大连*")

replace east1 = 1 if strmatch(location,"*济南*")

replace east2 = 1 if strmatch(location,"*无锡*")

replace east2 = 1 if strmatch(location,"*温州*")

replace east2 = 1 if strmatch(location,"*常州*")

replace east2 = 1 if strmatch(location,"*宁波*")

replace east2 = 1 if strmatch(location,"*南京*")

replace east2 = 1 if strmatch(location,"*苏州*")

replace east2 = 1 if strmatch(location,"*南通*")

replace east2 = 1 if strmatch(location,"*杭州*")

replace east2 = 1 if strmatch(location,"*连云港*")

replace east2 = 1 if strmatch(location,"*镇江*")

replace east3 = 1 if strmatch(location,"*惠州*")

replace east3 = 1 if strmatch(location,"*中山*")

replace east3 = 1 if strmatch(location,"*湛江*")

replace east3 = 1 if strmatch(location,"*珠海*")

replace east3 = 1 if strmatch(location,"*汕头*")

replace east3 = 1 if strmatch(location,"*深圳*")

replace east3 = 1 if strmatch(location,"*广州*")

replace east3 = 1 if strmatch(location,"*佛山*")

gen region = "西部" if west == 1

replace region = "东部" if east == 1

replace region = "中部" if central == 1

gen subregion = "环渤海湾经济圈" if east1 == 1

replace subregion = "长江三角洲经济区" if east2 == 1

replace subregion = "珠江三角洲经济区" if east3 == 1

keep if east1/east2/east3/central/west == 1

egen ownership = group(firmtype)

egen country = group(partner)

gen hs6 = substr(hs8, 1, 6)

g xx = trader

  bys hs6 country: egen totv = sum(value)
  bys hs6 country: egen totv_t = sum(value) if xx==1
  
collapse (mean) totv*, by(partner country hs6) fast
		
  foreach v of varlist totv* {
	replace `v' = 0 if `v' == .
			}
g s = totv_t/totv

label variable s "intermediary export share by country-hs6"

merge m:m partner using "C:\Local\数据\Table 3\disance+population+GDP+document.dta" 

drop _merge

merge m:m partner hs6 using "C:\Local\数据\Table 3\hs6-mfn.dta"

drop if _merge == 2

drop _merge

drop partner totv totv_t GDP2005 distance population

save "C:\Local\数据\Table 6A\table 6 data - east1/east2/east3/central/west.dta", replace

*/

use "C:\Local\数据\Table 6A\table 6 data - east1/east2/east3/central/west.dta",clear

/// Table 6A Panel A

areg s lnDistance lnGDP lnpopulation tradeproc mfn, a(hs6) cluster(country)

/// Table 6A Panel C

areg s lnDistance lnGDP lnpopulation SurveyData mfn, a(hs6) cluster(country)

areg s lnDistance lnGDP lnpopulation document mfn, a(hs6) cluster(country)

/// Table 6A Panel B

use "C:\Local\数据\Table 3\table 3 data.dta",clear

areg s lnDistance lnGDP lnpopulation SurveyData mfn, a(hs6) cluster(country)

areg s lnDistance lnGDP lnpopulation document mfn, a(hs6) cluster(country)

